package com.gitee.cc.system.controller;

import com.gitee.cc.common.vo.AjaxResult;
import com.gitee.cc.system.domain.User;
import com.gitee.cc.system.service.MenuService;
import com.gitee.cc.system.vo.FrontUser;
import com.gitee.cc.system.vo.MenuTree;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : cc
 * @date : Create in 13:28 2018/11/1
 */
@RestController
@Slf4j
public class AuthController {

    @Resource
    private MenuService menuService;

    @PostMapping("/auth/menus")
    public AjaxResult getMenu(@RequestBody User token){
        try {
            List<MenuTree> menus = menuService.getMenusByUsername(token);
            log.info("菜单数据构建成功={}",menus);
            return AjaxResult.success().put("menus", menus);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("菜单数据异常={}",e);
            return AjaxResult.error();
        }
    }
    @GetMapping("/auth/all/menus")
    public AjaxResult getAllMenu(){
        try {
            List<MenuTree> menus = menuService.getAllMenus();
            log.info("菜单数据构建成功={}",menus);
            return AjaxResult.success().put("menus", menus);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("菜单数据异常={}",e);
            return AjaxResult.error();
        }
    }
    @PostMapping("/auth/user/Info")
    public AjaxResult getUserInfo(@RequestBody User token){
        try {
            FrontUser frontUser = menuService.getFrontUser(token);
            log.info("用户信息={}",frontUser);
            return AjaxResult.success().put("userinfo",frontUser);
        } catch (Exception e) {
            log.error("获取用户信息异常={}", e);
            return AjaxResult.error();
        }
    }
}
